cmake_minimum_required(VERSION 3.20)

# Project definition
project(CppRuntimeShim
    VERSION 0.1.0
    DESCRIPTION "C++ runtime compatibility layer for C# to C++ conversion"
    LANGUAGES CXX
)

# C++ Standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Export compile commands for clangd and other tools
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()

# Options
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
option(BUILD_TESTING "Build tests" ON)
option(BUILD_BENCHMARKS "Build benchmarks" OFF)
option(BUILD_EXAMPLES "Build examples" ON)
option(ENABLE_COVERAGE "Enable code coverage" OFF)
option(ENABLE_SANITIZERS "Enable sanitizers (ASan, UBSan)" OFF)

# Global compiler flags
if(MSVC)
    add_compile_options(/W4 /WX /permissive-)
    # Enable multi-processor compilation
    add_compile_options(/MP)
else()
    add_compile_options(-Wall -Wextra -Wpedantic -Werror)
    # Additional warnings
    add_compile_options(-Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Wunused -Woverloaded-virtual)
    add_compile_options(-Wpedantic -Wconversion -Wsign-conversion -Wmisleading-indentation)
    add_compile_options(-Wduplicated-cond -Wduplicated-branches -Wlogical-op -Wnull-dereference)
    add_compile_options(-Wuseless-cast -Wdouble-promotion -Wformat=2)
endif()

# Sanitizers
if(ENABLE_SANITIZERS AND NOT MSVC)
    add_compile_options(-fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer)
    add_link_options(-fsanitize=address -fsanitize=undefined)
endif()

# Code coverage
if(ENABLE_COVERAGE AND NOT MSVC)
    add_compile_options(--coverage -O0 -g)
    add_link_options(--coverage)
endif()

# Include directories
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/include
)

# Subdirectories (to be added in Phase 1)
# add_subdirectory(src)
# add_subdirectory(include)

# Testing
if(BUILD_TESTING)
    enable_testing()
    # add_subdirectory(tests)
endif()

# Benchmarks
if(BUILD_BENCHMARKS)
    # add_subdirectory(benchmarks)
endif()

# Examples
if(BUILD_EXAMPLES)
    # add_subdirectory(examples)
endif()

# Installation rules (to be defined in Phase 1)
include(GNUInstallDirs)

# Install headers
# install(
#     DIRECTORY include/
#     DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
#     FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
# )

# CMake package config (to be defined in Phase 1)
# include(CMakePackageConfigHelpers)

# Print configuration summary
message(STATUS "")
message(STATUS "==================================================")
message(STATUS "C++ Runtime Shim Configuration Summary")
message(STATUS "==================================================")
message(STATUS "CMake version:        ${CMAKE_VERSION}")
message(STATUS "C++ compiler:         ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "C++ standard:         ${CMAKE_CXX_STANDARD}")
message(STATUS "Build type:           ${CMAKE_BUILD_TYPE}")
message(STATUS "Shared libs:          ${BUILD_SHARED_LIBS}")
message(STATUS "Build testing:        ${BUILD_TESTING}")
message(STATUS "Build benchmarks:     ${BUILD_BENCHMARKS}")
message(STATUS "Build examples:       ${BUILD_EXAMPLES}")
message(STATUS "Enable coverage:      ${ENABLE_COVERAGE}")
message(STATUS "Enable sanitizers:    ${ENABLE_SANITIZERS}")
message(STATUS "Install prefix:       ${CMAKE_INSTALL_PREFIX}")
message(STATUS "==================================================")
message(STATUS "")
